<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="角色名称" prop="roleName">
				<el-input v-model="form.roleName" clearable></el-input>
			</el-form-item>
			<el-form-item label="角色编码" prop="roleCode">
				<el-input v-model="form.roleCode" clearable></el-input>
			</el-form-item>
			<template v-if="mode!=='add'">
				<el-form-item label="角色类型" prop="roleType">
					<el-radio-group v-model="form.roleType" disabled clearable>
						<el-radio label="1">内置</el-radio>
						<el-radio label="2">新增</el-radio>
					</el-radio-group>
				</el-form-item>
			</template>
			<el-form-item label="是否有效" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!=='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			// 表单元素控制
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaving: false,
			// 表单数据
			form: {
				id: null,
				roleName: null,
				roleCode: null,
				roleType: '1',
				status: '1',
				remark: null
			},
			// 表单验证规则
			rules: {
				roleName: [
					{required: true, message: '请输入角色名称', trigger: 'blur'}
				],
				roleCode: [
					{required: true, message: '请输入角色编码', trigger: 'blur'}
				],
				roleType: [
					{required: true, message: '请选择角色类型', trigger: 'blur'}
				],
				status: [
					{required: true, message: '请选择状态', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {

	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		// 表单注入数据
		setData(data) {
			this.form.id = data.id;
			this.form.roleName = data.roleName;
			this.form.roleCode = data.roleCode;
			this.form.roleType = data.roleType;
			this.form.status = data.status;
			this.form.remark = data.remark;
		},
		// 表单提交
		submit() {
			this.$refs.dialogForm.validate((valid) => {
				if (valid) {
					this.isSaving = true;
					if (this.form.id) {
						this.$API.manage.role.edit.put(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					} else {
						this.$API.manage.role.add.post(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					}
				}
			})
		}
	}
}
</script>

<style>
</style>
